﻿// JScript 文件
//控制树的单选
var objSelect;    
function SingleSelect()
{
    var sr = getEventSender();
    if(sr.type == "checkbox")
    {        
        if (objSelect != null && objSelect.length > 0)
        {
            var isSrc = Include(sr, objSelect);//看事件源是不是来源于已选中的
            
            if (isSrc == false)
            {
                for (var i = 0; i < objSelect.length; i++)
                {
                    if (objSelect[i].checked == true)
                    {
                        objSelect[i].checked = false;
                        selectIDName(objSelect[i]);
                    }
                }
            }
        }        
        
        objSelect = SelectSameNode(sr);        
    }
}

//判断数级里是否包含某一个checkbox
function Include(chkSrc, chkArray)
{
    var isInclude = false;
    for (var i = 0; i < chkArray.length; i++)//看事件源是不是来源于已选中的
    {
        if (chkArray[i] == chkSrc)
        {
            isInclude = true;
            break;
        }
    }
    return isInclude;
}



//部门用户树的多选
function MultiSelect(chk)
{    
    if(chk == null)
    {
        chk = getEventSender();        
    }
    if(chk.type == "checkbox")
    {    
        SelectSameNode(chk);

        var divChild = getChildNodeDiv(chk);
        
        if(divChild != null)
        {
            var chks = divChild.getElementsByTagName("input");  
                     
            for(var i = 0;i < chks.length; i++)
            {
                if(chks[i].type.toLowerCase() == "checkbox")
                {
                    if(!chk.checked)
                    {                        
                        chks[i].disabled = "";
                    }
                    else
                    {
                        if (chks[i].checked)
                        {
                            chks[i].checked = false;
                            selectIDName(chks[i]);
                        }
                        
                        chks[i].disabled = "disabled";
                    }
                }
            }
        }
    }    
}

function SelectUser(chk)
{
    if(chk == null)
    {
        chk = getEventSender();     
    }
    if(chk.type == "checkbox")
    {    
        SelectSameNode(chk);
        
      
        var divChild = getChildNodeDiv(chk);
           
        if(divChild != null)
        {
            var chks = divChild.getElementsByTagName("input");           
            for(var i=0;i<chks.length;i++)
            {
                if(chks[i].type.toLowerCase()=="checkbox")
                {
                    if(chk.checked != chks[i].checked)
                    {                       
                        chks[i].checked = chk.checked;
                        selectIDName(chks[i]);
                    }                   
                }
            }
        }
    }   
}

//把相同的结点级联选中,主要用于一个用户属于多个部门
function SelectSameNode(chk)
{
    var id = chk.id;

    var sameNodes = document.getElementsByName(id);
    var arr = new Array();
    
    for (var i = 0; i < sameNodes.length; i++)
    {
        sameNodes[i].checked = chk.checked;
        arr.push(sameNodes[i]);
    }
    
    return arr;
    
}

function CascadSelect(tv)
{
    var divTree = document.getElementById(tv);
    var chk = getEventSender();
    if(chk.type != "checkbox")
    {
        return;
    }
    var childDiv = getChildNodeDiv(chk);
    if(childDiv != null)
    {
        var subChks = childDiv.getElementsByTagName("input");
      
        //当前节点的子节点与此节点同步
        for(var i=0;i<subChks.length;i++)
        {
            if(subChks[i].type == "checkbox")
            {
                if(subChks[i].checked != chk.checked)
                {
                    subChks[i].checked = chk.checked;
                    selectIDName(subChks[i]);
                }
            }
        }
    }
    
    //如当前节点选中，则所有父节点都选中        
    setParentNodes(chk);
}

//如当前节点选中，则所有父节点都选中
function setParentNodes(chk)
{
    if(chk.checked)
    {
        var upperid = chk.getAttribute("upperid");
     
        var upperNode = document.getElementById(upperid);
       
        if (upperNode != null)
        {
            upperNode.checked = true;
            selectIDName(upperNode);
            setParentNodes(upperNode);
        }
    }    
}

function getParentTbl(chk)
{
    var tbl = chk.parentNode;
    if(tbl.tagName.toUpperCase() != "TABLE")
    {
        tbl = getParentTbl(tbl);
    }
    return tbl;
}

//获得子节点DIV
function getChildNodeDiv(chk)
{    
    var tbl = getParentTbl(chk);

    var childDiv = tbl.nextSibling;
    if (childDiv != null && childDiv.tagName != null && childDiv.tagName.toLowerCase() == "div")
    {
        return childDiv;
    }
    else
    {
        return null;
    }
}



var selectedImg = null;

function SelectNode(strImg)
{
    
    if(selectedImg != null)
    {
        selectedImg.src = selectedImg.src.replace("folderopen.gif","folder.gif");
    }

    var img = document.getElementById(strImg);

    img.src = img.src.replace("folder.gif","folderopen.gif");
  
    selectedImg = img;
}

//设置用户选择的ID和Name
function selectIDName(chk)
{
    var hidID = document.getElementById("SelectIDs");
    var hidName = document.getElementById("SelectNames");

    var idValue = hidID.value;
    var nameValue = hidName.value;
    
    
    var strID = chk.id.substr(1, chk.id.length - 1);
    var strName = chk.nextSibling.innerHTML;
   
    

    if (chk.checked == true)
    {
        if (idValue == "")
        {
            idValue = ",";
            nameValue = ",";
        }
        
        if (idValue.indexOf("," + strID + ",") < 0)
        {
            idValue += strID + ",";
            nameValue += strName + ",";
        }
    }
    else
    {
        idValue = idValue.replace("," + strID + ",", ",");
        nameValue = nameValue.replace("," + strName + ",",",");
        if (idValue == ",")
        {
            idValue = "";
            nameValue = "";
        }
    }
    
    
    hidID.value = idValue;
    hidName.value = nameValue;
    
}

//初始化
function restoreTreeSelect(strSelectIDs)
{
    var ids = strSelectIDs.split(",");
    
    var arr = new Array();
   
    for (var i = 0; i < ids.length; i++)
    {
        var chks = document.getElementsByName("_" + ids[i]);
        if (chks != null && chks.length > 0)
        {            
            chks[0].checked = true;
            selectIDName(chks[0]);            
            arr = SelectSameNode(chks[0]);            
        }
    }
    
    objSelect = arr;//初始化一开始选中的节点
}

//树对话框返回数值
function returnDlgValue()
{
    var args = window.dialogArguments;
    
    args[0] = document.getElementById("SelectIDs").value;

    args[1] = document.getElementById("SelectNames").value;


    if (args[1] != "")
    {
        args[1] = args[1].substr(1, args[1].length - 2);
        args[0] = args[0].substr(1, args[0].length - 2);
    }
    window.close();
}

function returnUserValue()
{
    var args = window.dialogArguments;
    
    var ids = document.getElementById("SelectIDs").value.split(",");
    var values = document.getElementById("SelectNames").value.split(",");

    var strid = "";
    var strvalue = "";
    for (var i = 0; i < ids.length; i++)
    {
        if (ids[i] != "")
        {
            if (parseInt(ids[i]) < 0)
            {
                strid += "," + ids[i];
                strvalue += "," + values[i];
            }
        }
    }

    if (strid != "")
    {
        strid = strid.substr(1, strid.length - 1);
        strvalue = strvalue.substr(1, strvalue.length - 1);
    }

    args[0] = strid;
    args[1] = strvalue;

    window.close();
}